/**
 * 
 */
package com.nari.heatshare.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.heatshare.model.IHeatMeterTypeParamDict;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2014-6-23
 */
public class HeatMeterTypeParamDictDaoImpl extends GenericDaoImpl<IHeatMeterTypeParamDict, String>
		implements IHeatMeterTypeParamDictDao {
	
	/**
	 * 分页查询热表型号参数配置列表
	 * @param typeCode 热量表型号编号
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-6-23
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatMeterTypeParamDict> queryTypeParamPage(String typeCode, 
			PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatMeterTypeParamDict.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatMeterTypeParamDict.class);
		//热量表型号编号
		if (typeCode!=null && !"".equals(typeCode.trim())) {
			listCriteria.add(Restrictions.ilike("typeCode", typeCode.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("typeCode", typeCode.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		// 统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		// 结果集排序
		listCriteria.addOrder(Order.asc("typeCode"));
		// 结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		// 构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatMeterTypeParamDict> typeParamPage = new ResultWithPageCond<IHeatMeterTypeParamDict>();
		typeParamPage.setList(listCriteria.list());
		typeParamPage.setPageCond(pageCond);
		return typeParamPage;
	}
	
	/**
	 * 根据型号编号统计型号参数的配置个数
	 * @param typeCode
	 * @param typeParamUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-6-23
	 */
	public int countTypeParamByTypeCode(String typeCode, String typeParamUID) throws Exception {
		String hsql = "select count(*) from IHeatMeterTypeParamDict hmtpd " +
				"where hmtpd.typeCode='"+typeCode+"'";
		if(typeParamUID!=null && !"".equals(typeParamUID)) {
			hsql += " and hmtpd.id<>'"+typeParamUID+"'";
		}
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}
	
	/**
	 * 通过热量表型号编号查询其型号参数配置
	 * @param typeCode
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-6-24
	 */
	public IHeatMeterTypeParamDict queryTypeParamByCode(String typeCode) throws Exception {
		String hsql = "from IHeatMeterTypeParamDict hmtpd " +
				"where hmtpd.typeCode='"+typeCode+"'";
		List<IHeatMeterTypeParamDict> typeParamList = this.runQueryHSql(hsql);
		if(typeParamList!=null && typeParamList.size()>0) {
			return typeParamList.get(0);
		}
		return null;
	}
}
